home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / BTCLASS.ZIP / EXAMPLE@.EXE / EXAMPLES.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-11  |  12.5 KB  |  467 lines

  1. /*//////////////////////////////////////////////////////////////////////////
  2. ///            ___                                                       ///
  3. ///          /_____\                                                     ///
  4. ///         |       |                 Copyright (c) 1991                 ///
  5. ///         |   R   |                                                    ///
  6. ///     ----|_______|----                     by                         ///
  7. ///   /------/ | | \------\                                              ///
  8. ///  |       | | | |       |      --  Object Resource Group  --          ///
  9. ///  |   O   | | | |   G   |                                             ///
  10. ///  |       |/   \|       |          4323 Brown Suite 249               ///
  11. ///   -------       -------            Dallas,  TX  75219                ///
  12. ///   Object Resource Group                                              ///
  13. ///                                      (214) 528-2745                  ///
  14. ///                                                                      ///
  15. ///                                    All Rights Reserved.              ///
  16. ///                                                                      ///
  17. //////////////////////////////////////////////////////////////////////////*/
  18.  
  19. //
  20. //  A small example program using the ORG Btrieve classes
  21. //
  22. #include <iostream.h>
  23. #include <iomanip.h>
  24. #include <strstrea.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #include "btdset.hpp"
  29. #include "btskey.hpp"
  30. #include "btdef.hpp"
  31. #include "bttran.hpp"
  32. #include "examples.hpp"
  33.  
  34.  
  35. ///////////////////////////////////////////////////////////////////////
  36. //////////////////////// Internal Prototypes //////////////////////////
  37. ///////////////////////////////////////////////////////////////////////
  38.  
  39. int CreatePersonDB();
  40. int CreateColorDB();
  41. int GetAndSetColors(PersonDB &personDB, ColorDB &colorDB);
  42. int SetColor(Person &person, ColorDB &colorDB);
  43.  
  44.  
  45. ///////////////////////////////////////////////////////////////////////
  46. /////////////////////// Class Implementations /////////////////////////
  47. ///////////////////////////////////////////////////////////////////////
  48.  
  49. // Normally class implementations are seperate from main file but
  50. // for convenience of exapmles I have kept them together
  51.  
  52. // Implementation of PersonDB
  53.  
  54. // Dump - dumps in person order every one in file
  55. int PersonDB::Dump()
  56.    {
  57.    BT_Key *nameKey = GetKey(0);
  58.    Person person(this);
  59.    int status=nameKey->GetFirst(&person);
  60.    int count=0;
  61.  
  62.    cout << "\n\n                   -----Dump of PERSON.DB-----\n";
  63.    cout << "\n------------- Name ----------------     --- Color Preference ---\n";
  64.  
  65.    char buffer[80];
  66.    while (!status)
  67.       {
  68.       count++;
  69.  
  70.       // the purpose of the brackets is to provide scope for the
  71.       //    ostrstream object
  72.       {
  73.       ostrstream buff(buffer,80);
  74.       buff << setiosflags(ios::left) << setw(35) << person.Name();
  75.       buff << "     " << person.ColorPreference() << endl << ends;
  76.       cout << buffer;
  77.       }
  78.  
  79.       status=nameKey->GetNext(&person);
  80.       }
  81.    cout << "\nTotal Number of People: " << count << "\n\n";
  82.  
  83.    if (status == BSTAT_EOF) status=0;
  84.    return status;
  85.    }
  86.  
  87.  
  88. // ColorDump - dumps in color order every one in file
  89. int PersonDB::ColorDump()
  90.    {
  91.    BT_SuppKeyDef keyDef;
  92.  
  93.    int status = keyDef.AddFinalKeySegment(37, 26, BKEY_EXTENDED+BKEY_DUPLICATE,
  94.                      BTYPE_ZSTRING);
  95.  
  96.    BT_SuppKey *colorKey = GetSuppKey(keyDef);
  97.  
  98.    Person person(this);
  99.    status=colorKey->GetFirst(&person);
  100.    int count=0;
  101.  
  102.    cout << "\n\n                -----Color Dump of PERSON.DB-----\n";
  103.    cout << "\n------------- Name ----------------     --- Color Preference ---\n";
  104.  
  105.    char buffer[80];
  106.    while (!status)
  107.       {
  108.       count++;
  109.  
  110.       {
  111.       ostrstream buff(buffer,80);
  112.       buff << setiosflags(ios::left) << setw(35) << person.Name();
  113.       buff << "     " << person.ColorPreference() << endl << ends;
  114.       cout << buffer;
  115.       }
  116.  
  117.       status=colorKey->GetNext(&person);
  118.       }
  119.  
  120.    cout << "\nTotal Number of People: " << count << "\n\n";
  121.  
  122.    if (!status || status == BSTAT_EOF)
  123.      status=colorKey->DropKey();
  124.    else
  125.       colorKey->DropKey();    // just drop it anyway but return previous
  126.                 // status.
  127.    return status;
  128.    }
  129.  
  130.  
  131. // Implementation of ColorDB
  132.  
  133. // Dump - dumps in person order every one in file
  134. int ColorDB::Dump()
  135.    {
  136.    BT_Key *shadeKey = GetKey(0);
  137.    Color color(this);
  138.    int status=shadeKey->GetFirst(&color);
  139.    int count=0;
  140.  
  141.    cout << "\n\n             -----Dump of COLOR.DB-----\n";
  142.    cout << "\n----- Color Shade -----    - Preference Count -\n";
  143.  
  144.    char buffer[80];
  145.    while (!status)
  146.       {
  147.       count++;
  148.  
  149.       {
  150.       ostrstream buff(buffer,80);
  151.       buff << setiosflags(ios::left) << setw(23) << color.Shade();
  152.       buff << "    " << color.PreferenceCount() << endl << ends;
  153.       cout << buffer;
  154.       }
  155.  
  156.  
  157.       status=shadeKey->GetNext(&color);
  158.       }
  159.    cout << "\nTotal Number of Colors: " << count << "\n\n";
  160.  
  161.    if (status == BSTAT_EOF) status=0;
  162.    return status;
  163.    }
  164.  
  165.  
  166. int ColorDB::DumpFromHighestCount()
  167.    {
  168.    // silly example to demonstrate GetPosition and Restore Position
  169.    BT_Key *shadeKey = GetKey(0);
  170.    BT_Key *countKey = GetKey(1);
  171.    Color color(this);
  172.    int count=0;
  173.    int status=countKey->GetLast();
  174.  
  175.    if (!status)
  176.       {
  177.       ULONG position = GetPosition();
  178.       status = shadeKey->RestorePosition(position, &color);
  179.       // in the above case as only one position was gotton this is also
  180.       // correct:
  181.       //     status = shadeKey->RestorePosition();
  182.       // it will restore to the position of the last GetPosition call
  183.  
  184.       cout << "\n\n -----Dump of COLOR.DB After Highest Count-----\n";
  185.       cout << "\n----- Color Shade -----    --- Preference Count ---\n";
  186.  
  187.       char buffer[80];
  188.       while (!status)
  189.      {
  190.      count++;
  191.  
  192.      {
  193.      ostrstream buff(buffer,80);
  194.      buff << setiosflags(ios::left) << setw(23) << color.Shade();
  195.      buff << "    " << color.PreferenceCount() << endl << ends;
  196.      cout << buffer;
  197.      }
  198.  
  199.      status=shadeKey->GetNext(&color);
  200.      }
  201.       cout << "\nTotal Number of Colors After Highest Count: " << count << "\n\n";
  202.       }
  203.  
  204.    if (status == BSTAT_EOF) status=0;
  205.    return status;
  206.    }
  207.  
  208.  
  209. // Implementation of Person
  210.  
  211. // Person(name, personDB) - constructor to connect a file to person and gets
  212. //                the name
  213. Person::Person(char *_name, PersonDB *_personDB) : personDB(_personDB)
  214.    {
  215.    SetName(_name);
  216.    GetRec();
  217.    }
  218.  
  219. // GetRec() - gets a record associated with objects name
  220. int Person::GetRec()
  221.    {
  222.    BT_Key *nameKey = personDB->GetKey(0);
  223.    int status = nameKey->GetEqual(Name(), &data);
  224.    return status;
  225.    }
  226.  
  227. // Add() - writes a record to the file
  228. int Person::Add()
  229.    {
  230.    BT_Key *nameKey = personDB->GetKey(0);
  231.    int status = nameKey->Insert(&data);
  232.    return status;
  233.    }
  234.  
  235.  
  236. // Implementation of Color
  237.  
  238. // Color(_name, personDB) - constructor to connect a file to color and get
  239. //                the shade
  240. Color::Color(char *_name, ColorDB *_colorDB) : colorDB(_colorDB)
  241.    {
  242.    SetShade(_name);
  243.    GetRec();
  244.    }
  245.  
  246. // GetRec() - gets a record associated with objects name
  247. int Color::GetRec()
  248.    {
  249.    BT_Key *shadeKey = colorDB->GetKey(0);
  250.    int status = shadeKey->GetEqual(Shade(), &data);
  251.    return status;
  252.    }
  253.  
  254. // Add() - writes a record to the file
  255. int Color::Add()
  256.    {
  257.    BT_Key *shadeKey = colorDB->GetKey(0);
  258.    int status = shadeKey->Insert(&data);
  259.    return status;
  260.    }
  261.  
  262. // Update() - writes a record to the file
  263. int Color::Update()
  264.    {
  265.    BT_Key *shadeKey = colorDB->GetKey(0);
  266.    int status = shadeKey->Update(&data);
  267.    return status;
  268.    }
  269.  
  270.  
  271. ///////////////////////////////////////////////////////////////////////
  272. /////////////////////////////// MAIN ///////////////////////////////////
  273. ////////////////////////////////////////////////////////////////////////
  274.  
  275.  
  276. /////////// Btrieve shell is already running, right ? //////////
  277.  
  278. main()
  279.     {
  280.     int status;
  281.     // create the file
  282.     CreatePersonDB();
  283.     CreateColorDB();
  284.  
  285.     // open the files
  286.     PersonDB personDB("person.db");
  287.     ColorDB colorDB("color.db");
  288.  
  289.     if (!personDB.Status() && !colorDB.Status()) // opened successfully
  290.        {
  291.        // poll the user for names and color preferences
  292.        status = GetAndSetColors(personDB, colorDB);
  293.        }
  294.  
  295.     // print out the results
  296.  
  297.     if (!status)
  298.        status = personDB.Dump();
  299.  
  300.     if (!status)
  301.        status = personDB.ColorDump();
  302.  
  303.  
  304.     if (!status)
  305.        status = colorDB.Dump();
  306.  
  307.     if (!status)
  308.        status = colorDB.DumpFromHighestCount();
  309.  
  310.     ///// The file is closed when the destructor of person is called /////
  311.     if (status)
  312.        cout << "\nProgram terminated with error: " << status << "\n";
  313.     else
  314.        cout << "Program terminated successfully.\n";
  315.  
  316.     return 0;
  317.     }
  318.  
  319. // Create - Creates the person file
  320. int CreatePersonDB()
  321.    {
  322.    int status=0;
  323.  
  324.    BT_Def fileDef(sizeof(PersonData), 0);
  325.  
  326.    ///// Define the Keys /////
  327.  
  328.    status = fileDef.AddFinalKeySegment(1, 36, BKEY_EXTENDED+
  329.                           BKEY_MODIFIABLE+
  330.                           BKEY_ALT_SEQUENCE,
  331.                           BTYPE_ZSTRING);
  332.    if (!status)
  333.       {
  334.       ///// set alternate collating sequence to the standard UPPER.ALT /////
  335.       fileDef.SetUpperAlt();
  336.  
  337.       ///// This actually creates the dataset /////
  338.       status = fileDef.CreateBtrieve("person.db");
  339.       }
  340.  
  341.    return(status);
  342.    }
  343.  
  344. // Create - Creates the color file
  345. int CreateColorDB()
  346.    {
  347.    int status=0;
  348.  
  349.    BT_Def fileDef(sizeof(ColorData), 0);
  350.  
  351.    ///// Define the Keys /////
  352.  
  353.    status = fileDef.AddFinalKeySegment(1, 26, BKEY_EXTENDED+
  354.                           BKEY_MODIFIABLE,
  355.                           BTYPE_ZSTRING);
  356.    if (!status)
  357.       status = fileDef.AddFinalKeySegment(27, 2, BKEY_BINARY+
  358.                           BKEY_MODIFIABLE+
  359.                           BKEY_DUPLICATE,
  360.                           BTYPE_INTEGER);
  361.    if (!status)
  362.       {
  363.       ///// This actually creates the dataset /////
  364.       status = fileDef.CreateBtrieve("color.db");
  365.       }
  366.  
  367.    return(status);
  368.    }
  369.  
  370. int GetAndSetColors(PersonDB &personDB, ColorDB &colorDB)
  371.    {
  372.    int status=0;
  373.  
  374.    // loop until no name is given
  375.    char name[36];
  376.    do
  377.       {
  378.       cout << "\nPlease enter person's name (or 'END'): ";
  379.       cin >> name;
  380.  
  381.       if ( strcmp(name,"END") )
  382.      {
  383.      // attempt to find in file
  384.      Person person(name, &personDB);// constructor gets name if possible
  385.                     // from personDB.
  386.  
  387.      status = person.Status();    // Because constructors can't return
  388.                     // a status we use status of file
  389.                     // operation.  There are other ways
  390.                     // but this is a simple method.
  391.      if (status == BSTAT_NOTFOUND)
  392.         {
  393.         // this name is not in the file so get color and add it
  394.         status = SetColor(person, colorDB);
  395.         }
  396.      else if (!status)
  397.         {
  398.         cout << name << "'s color preference is " <<
  399.                person.ColorPreference() << "\n\n";
  400.         }
  401.      }
  402.       else
  403.       break;        // No Name - Exit Condition
  404.       } while (!status);
  405.  
  406.    return status;
  407.    }
  408.  
  409. int SetColor(Person &person, ColorDB &colorDB)
  410.    {
  411.    char colorPref[26];
  412.    int status=0;
  413.  
  414.    // enter information
  415.    cout << "Please enter " << person.Name() << "'s Color Preference: ";
  416.    cin >> colorPref;
  417.  
  418.    // if color was entered continue
  419.    if (strlen(colorPref))
  420.       {
  421.       // capitalize on the color
  422.       strupr(colorPref);
  423.  
  424.       // this is the goal:
  425.       //    start a transaction
  426.       //    if color record exists in color file,
  427.       //       get it and increment its prefCount by one
  428.       //    else
  429.       //       add the color record to color file and set count to one
  430.       //    insert the person record
  431.       //    if successful
  432.       //       end the transaction
  433.       //    else
  434.       //       abort transaction
  435.  
  436.       BT_Transaction transaction;
  437.       if (!transaction.Status())        // creation was successful
  438.      {
  439.      Color color(colorPref, &colorDB);    // gets the color record
  440.      if (!color.Status())        // record was found
  441.         {
  442.         color.IncPrefCount();
  443.         status = color.Update();
  444.         }
  445.      else if (color.Status() == BSTAT_NOTFOUND) // record not found
  446.         {
  447.         // note that the constructor already set the shade
  448.         color.SetPrefCount(1);
  449.         status = color.Add();
  450.         }
  451.  
  452.      if (!status)
  453.         {
  454.         person.SetColorPreference(colorPref);
  455.         status = person.Add();
  456.         }
  457.  
  458.      // Commit if everything succeeded
  459.      if (!status)
  460.         status = transaction.Commit();
  461.      else
  462.         transaction.RollBack();
  463.      }
  464.       }
  465.    return status;
  466.    }
  467.